﻿using System.IO.Ports;

namespace 燃油加注机指令翻译
{
    public partial class 一键加油 : Form
    {
        SerialPort serialPort;
        public 一键加油()
        {
            InitializeComponent();
        }
        private void 一键加油_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }
        void sendData(string data)
        {
            byte[] bytes = Tool.HexStringToByteArray(data.Replace(" ", ""));
            serialPort.Write(bytes, 0, bytes.Length);
        }
        private void ComConnection()
        {
            try
            {
                serialPort = new SerialPort();
                // 设置串口参数
                serialPort.PortName = comboBox1.SelectedItem.ToString(); // 串口端口号，根据实际情况设置
                serialPort.BaudRate = 9600; // 波特率，根据实际情况设置
                serialPort.Parity = Parity.Even; // 校验位 偶校验
                serialPort.DataBits = 8; // 数据位
                serialPort.StopBits = StopBits.One; // 停止位

                serialPort.Open();

                if (serialPort.IsOpen)
                {
                    button7.Text = "已连接";
                    button7.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加油机连接失败：" + ex.Message);

            }
        }

        static string ConvertDecimalToHex(string decimalValue, int intLength, int decimalLength)
        {
            intLength *= 2;
            decimalLength *= 2;
            string format = "";
            for (int i = 0; i < intLength; i++)
            {
                format += "0";
            }
            format += ".";
            for (int i = 0; i < decimalLength; i++)
            {
                format += "0";
            }
            decimalValue = decimal.Parse(decimalValue).ToString(format);
            string intPart = decimalValue.Split(".")[0];
            string decimalPart = decimalValue.Split(".")[1];
            return intPart.PadLeft(intLength, '0') + decimalPart.PadRight(decimalLength, '0');
        }

        private void Price_TextChanged(object sender, EventArgs e)
        {
            if (Price.Text != "" && double.Parse(Price.Text) != 0 && Litter.Text != "" && double.Parse(Litter.Text) != 0)
            {
                Amount.Text = (double.Parse(Price.Text) * double.Parse(Litter.Text)).ToString();
            }
        }

        private void Litter_TextChanged(object sender, EventArgs e)
        {
            if (Price.Text != "" && double.Parse(Price.Text) != 0 && Litter.Text != "" && double.Parse(Litter.Text) != 0)
            {
                Amount.Text = (double.Parse(Price.Text) * double.Parse(Litter.Text)).ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Price.Text == "" || decimal.Parse(Price.Text).CompareTo(0) == 0)
            {
                MessageBox.Show("单价不正确");
                return;
            }
            if (Litter.Text == "" || decimal.Parse(Litter.Text).CompareTo(0) == 0)
            {
                MessageBox.Show("加注量不正确");
                return;
            }
            if (serialPort.IsOpen)
            {
                Cancel cancelRefresh = new Cancel();
                cancelRefresh.Chain = 0.ToString().PadLeft(4, '0');
                sendData(cancelRefresh.instructionsHex());
                Thread.Sleep(500);
                Addition additionRefresh = new Addition();
                additionRefresh.Litter = ConvertDecimalToHex("0", 2, 1);
                additionRefresh.Amount = ConvertDecimalToHex("0", 2, 1);
                additionRefresh.Kilogram = ConvertDecimalToHex("0000.00", 2, 1);
                additionRefresh.Price = ConvertDecimalToHex(Price.Text, 1, 2);
                additionRefresh.Reciprocal = ConvertDecimalToHex("00.000000", 1, 3);
                additionRefresh.Flag = 0.ToString().PadLeft(2, '0');
                additionRefresh.Chain = 9999.ToString().PadLeft(4, '0');
                sendData(additionRefresh.instructionsHex());
                Thread.Sleep(500);
                Cancel cancel = new Cancel();
                cancel.Chain = 9999.ToString().PadLeft(4, '0');
                sendData(cancel.instructionsHex());
                Thread.Sleep(500);
                Addition addition = new Addition();
                addition.Litter = ConvertDecimalToHex(Litter.Text, 2, 1);
                addition.Amount = ConvertDecimalToHex(Amount.Text, 2, 1);
                addition.Kilogram = ConvertDecimalToHex("0000.00", 2, 1);
                addition.Price = ConvertDecimalToHex(Price.Text, 1, 2);
                addition.Reciprocal = ConvertDecimalToHex("00.000000", 1, 3);
                addition.Flag = 0.ToString().PadLeft(2, '0');
                addition.Chain = 0.ToString().PadLeft(4, '0');
                sendData(addition.instructionsHex());
            }
            else
            {
                MessageBox.Show("请先打开连接");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.ComConnection();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (serialPort.IsOpen)
            {
                button7.Text = "连接";
                button7.Enabled = true;
                serialPort.Close();
            }
        }
    }
}
